//
//  STWXPublishViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/25.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STWXPublishViewModel.h"

#import "ArticleInfoData.h"

@interface STWXPublishViewModel()

@property (nonatomic, strong) ProvinceListData* zoneData;

@end

@implementation STWXPublishViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    DataManager* dm = [DataManager sharedDataManager];
    
    self.zoneData = dm.locationPLData;
    
    RACSignal* zoneChangeSignal = [RACObserve(self, zoneData) distinctUntilChanged];
    [[zoneChangeSignal replay] subscribeNext:^(id x)
     {
         @strongify(self);
         
         ProvinceListData* curPListData = x;
         
         if (x == nil)
         {
             self.area = @"未能定位";
         }
         else
         {
             self.area = curPListData.name;
         }
     }];
    
    self.detailCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        @weakify(self);
        return [self _requestDetailSignal:self.articleID];
    }];
    
    self.submitCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        @weakify(self);
        return [[self _checkSubmitData] then:^RACSignal *
        {
            @strongify(self);
            
            return [self _requestSubmitSignal];
        }];
    }];
}

- (void)resetZoneData:(ProvinceListData*)zoneData
{
    self.zoneData = zoneData;
}

- (RACSignal*)_checkSubmitData
{
    NSString* result = @"";
    if (![result isNotBlank] && ![self.ptitle isNotBlank])
    {
        result = @"请填写标题";
    }
    else if (![result isNotBlank] && ![self.pintro isNotBlank])
    {
        result = @"请填写简介";
    }
    else if (![result isNotBlank] && ![self.content isNotBlank])
    {
        result = @"请填写内容";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
}

- (RACSignal *)_requestDetailSignal:(NSString*)articleID
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* userID = [um isLogined] ? um.userData.userID : @"";
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_articledetail params:interface_param_articledetail(articleID, userID)] subscribeNext:^(id x)
                 {
                     @strongify(self);
                     ArticleInfoData* data = [ArticleInfoData modelWithJSON:x];
                     
                     self.ptitle = data.title;
                     self.pintro = data.summary;
                     self.content = data.content;
                     
                     [subscriber sendNext:nil];
                     [subscriber sendCompleted];
                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"请求详情失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

- (RACSignal*)_requestSubmitSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* userID = um.userData.userID;
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
//                NSString* areaID = self.zoneData ? self.zoneData.areaID : @"";
                NSString* areaID = um.userData.areaID;
                
                @weakify(self);
                
                if (self.articleID && [self.articleID isNotBlank])
                {
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_stwx_wordedit params:interface_param_stwx_wordedit(userID, self.ptitle, self.content, self.pintro, self.articleID)] subscribeNext:^(id x)
                     {
                         
                         [subscriber sendNext:x[SUCommandSysMessageKey]];
                         [subscriber sendCompleted];
                    
                     } error:^(NSError *error)
                     {
                         NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"编辑生态文献失败";
                         [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                         [subscriber sendCompleted];
                     }];
                }
                else
                {
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_stwxupload params:interface_param_stwxupload(userID, self.ptitle, self.content, self.pintro, areaID)] subscribeNext:^(id x)
                     {
                         [subscriber sendNext:x[SUCommandSysMessageKey]];
                         [subscriber sendCompleted];
                         
                     } error:^(NSError *error)
                     {
                         NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"提交生态文献失败";
                         [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                         [subscriber sendCompleted];
                     }];
                }
                
                return nil;
            }];
}

@end
